package com.scujj.cs6.controller;

import com.scujj.cs6.annotation.AuthCheck;
import com.scujj.cs6.common.Result;
import com.scujj.cs6.model.dto.user.UserEmailLoginDTO;
import com.scujj.cs6.model.dto.user.UserInfoUpdateDTO;
import com.scujj.cs6.model.dto.user.UserPhoneLoginDTO;
import com.scujj.cs6.model.dto.user.UserRegisterDTO;
import com.scujj.cs6.model.vo.UserSimpleVo;
import com.scujj.cs6.model.vo.UserVo;
import com.scujj.cs6.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户接口", description = "用户注册、登录、信息获取相关API")
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    @Operation(summary = "获取用户详细信息", description = "根据token获取用户的详细信息")
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @GetMapping("/detail")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = UserVo.class)
            )
    )
    public Result<UserVo> getUserDetailByToken(HttpServletRequest request) {
        return Result.success(userService.getUserDetailByToken(request));
    }

    @Operation(summary = "获取用户详细信息", description = "根据id获取用户的详细信息")
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @GetMapping("/detail/{id}")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = UserVo.class)
            )
    )
    public Result<UserVo> getUserDetailById(@Parameter(name = "id" , description = "用户ID", required = true , in = ParameterIn.PATH) @PathVariable Long id) {
        return Result.success(userService.getUserDetailById(id));
    }


    @Operation(summary = "获取用户简单信息", description = "根据token获取用户的简单信息")
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @GetMapping("/info")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = UserSimpleVo.class)
            )
    )
    public Result<UserSimpleVo> getUserByToken(HttpServletRequest request) {
        return Result.success(userService.getUserByToken(request));
    }

    @Operation(summary = "获取用户简单信息", description = "根据用户id获取用户的简单信息")
    @GetMapping("/info/{id}")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = UserSimpleVo.class)
            )
    )
    public Result<UserSimpleVo> getUserById(@Parameter(name = "id" , description = "用户ID", required = true , in = ParameterIn.PATH) @PathVariable Long id) {
        return Result.success(userService.getUserById(id));
    }

    @Operation(summary = "邮箱登录", description = "用户通过邮箱和密码登录")
    @PostMapping("/email")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            ),
            headers = {
                    @io.swagger.v3.oas.annotations.headers.Header(name = "Authorization", description = "认证信息", required = true)
            }
    )
    public Result<Void> emailLogin(@Valid @RequestBody UserEmailLoginDTO loginDTO,
                                   HttpServletRequest request,
                                   HttpServletResponse response) {
        userService.emailLogin(loginDTO,request,response);
        return Result.success();
    }

    @Operation(summary = "手机号登录", description = "用户通过手机号和密码登录")
    @PostMapping("/phone")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            ),
            headers = {
                    @io.swagger.v3.oas.annotations.headers.Header(name = "Authorization", description = "认证信息", required = true)
            }
    )
    public Result<Void> phoneLogin(@Valid @RequestBody UserPhoneLoginDTO loginDTO,
                                   HttpServletRequest request,
                                   HttpServletResponse response) {
        userService.phoneLogin(loginDTO,request,response);
        return Result.success();
    }

    @Operation(summary = "注册用户", description = "用户通过手机号或电子邮件注册")
    @PostMapping("/register")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            )
    )
    public Result<Void> registerUser(@Valid @RequestBody UserRegisterDTO registerDTO) {
        userService.createUser(registerDTO);
        return Result.success();
    }

    @Operation(summary = "退出登录", description = "用户退出登录")
    @AuthCheck
    @DeleteMapping("/logout")
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            )
    )
    public Result<Void> logout(HttpServletRequest request, HttpServletResponse response) {
        userService.logout(request,response);
        return Result.success();
    }

    @Operation(summary = "更新用户信息", description = "更新用户信息")
    @AuthCheck
    @PutMapping("/")
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            )
    )
    public Result<Void> updateUser(@Valid @RequestBody UserInfoUpdateDTO userInfoUpdateDTO , HttpServletRequest request) {
        userService.updateUser(userInfoUpdateDTO,request);
        return Result.success();
    }

}